package com.csj.mvvmtest.adapter

import android.view.LayoutInflater
import android.view.View
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.csj.mvvm.utils.ToastUtil
import com.csj.mvvmtest.R
import com.csj.mvvmtest.bean.ThirdBean
import com.csj.mvvmtest.databinding.ItemFlBinding
import com.csj.mvvmtest.widget.FlowGroupView


class ThirdAdapter2(data: List<ThirdBean.DataBean>) :
    BaseQuickAdapter<ThirdBean.DataBean, BaseViewHolder>(R.layout.item_third, data) {

    override fun convert(helper: BaseViewHolder, item: ThirdBean.DataBean) {
        helper.setText(R.id.title, item.name)
        val vv = helper.getView<FlowGroupView>(R.id.fl)
        vv.removeAllViews()
        for (s in item.children) {
            addTextView(vv, s)
        }
    }

    private fun addTextView(
        view: FlowGroupView,
        s: ThirdBean.DataBean.ChildrenBean
    ) {
        val child = ItemFlBinding.inflate(LayoutInflater.from(view.context), view, false)
        child.tv.text = s.name
        initEvents(child.root, s) //监听
        view.addView(child.root)
    }

    /**
     * 为每个view 添加点击事件
     */
    private fun initEvents(
        tv: View,
        s: ThirdBean.DataBean.ChildrenBean
    ) {
        tv.setOnClickListener {
            ToastUtil.showToast(s.name)
        }
    }


}